<template>
	<view class="fruit-admin-add-item-container">
		<view class="fruit-admin-add-item-item">
			<view>商品名称</view>
			<input type="text" placeholder="请输入商品名称" v-model="data.fruitItemInfo.title">
		</view>
		<view class="fruit-admin-add-item-item">
			<view>商品描述</view>
			<textarea placeholder="请输入商品描述" v-model="data.fruitItemInfo.desc"></textarea>
		</view>
		<view class="fruit-admin-add-item-item">
			<view>商品原价格</view>
			<input type="text" placeholder="请输入商品原价格" v-model="data.fruitItemInfo.price">
		</view>
		<view class="fruit-admin-add-item-item">
			<view>商品折后价</view>
			<input type="text" placeholder="请输入商品折后价" v-model="data.fruitItemInfo.after_price">
		</view>
	</view>
	<button class="fruit-admin-add-btn" @tap="sendData">修改商品</button>
</template>

<script>
	import {onLoad} from '@dcloudio/uni-app'
	import {reactive} from 'vue'
	import global from "../../config/global.js"
	export default {
		name:"fruit-admin-edit-item",
		setup(){
			let data = reactive({
				fruitItemInfo:{
					id:0,
					title:"",
					desc:"",
					price:"",
					after_price:"",
					kindId:0,
					pic_url:""
				}
			})
			
			function select(e){
				data.fruitItemInfo.pic_url = e.tempFilePaths[0]
			}
			
			onLoad((e)=>{
				let obj = JSON.parse(decodeURIComponent(e.fruitItem))
				data.fruitItemInfo.title = obj.title
				data.fruitItemInfo.desc = obj.info
				data.fruitItemInfo.price = obj.price
				data.fruitItemInfo.after_price = obj.discount_price
				data.fruitItemInfo.pic_url = obj.pic_url
				data.fruitItemInfo.kindId = obj.fruit_buy_kind_id
				data.fruitItemInfo.id = obj.id
			})
			
			function sendData(){
				console.log(data.fruitItemInfo)
				uni.request({
					url:`${global.BASE_URL}/fruit/reviseFruitItem`,
					method:"PUT",
					header:{
						"mytoken":uni.getStorageSync("localtoken")
					},
					data:[{
						"id":data.fruitItemInfo.id,
						"fruit_buy_kind_id":data.fruitItemInfo.kindId,
						"title":data.fruitItemInfo.title,
						"info":data.fruitItemInfo.desc,
						"price":data.fruitItemInfo.price,
						"discount_price":data.fruitItemInfo.after_price,
						"pic_url":data.fruitItemInfo.pic_url
					}],
					success: (res) => {
						console.log(res)
						if(res.data.code){
							uni.navigateBack()
						}
					},
					fail: (err) => {
						console.log(err)
					}
				})
			}
			
			return {
				data,
				sendData,
				select
			}
		}
	}
</script>

<style scoped lang="scss">
	.fruit-admin-add-item-container{
		width: 680rpx;
		margin: 15rpx 35rpx;
		
		& .fruit-admin-add-item-item{
			display: flex;
			margin: 20rpx 0;
			padding: 10rpx 0;
			border-bottom: 1rpx solid #bbbbbb;
			
			& view{
				font-size: 30rpx;
				width: 150rpx;
				line-height: 40rpx;
			}
			
			& input {
				font-size: 30rpx;
				width: 500rpx;
				line-height: 40rpx;
				margin-left: 10rpx;
				padding: 0 10rpx;
			}
			
			& textarea{
				font-size: 30rpx;
				width: 500rpx;
				line-height: 40rpx;
				height: 120rpx;
				margin-left: 10rpx;
				padding: 0 10rpx;
			}
			
		}
		
	}
	
	.fruit-admin-add-btn{
		background-color: #9BCDD2;
		color: white;
		width: 400rpx;
		margin: 50rpx 175rpx 10rpx 175rpx;
		height: 70rpx;
		line-height: 70rpx;
		font-size: 35rpx;
		// font-weight: bold;
	}
</style>
